package GUI;



import java.awt.Color;
import java.awt.Font;
import java.awt.Point;

import javax.swing.ImageIcon;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


import java.awt.Dimension;
import java.io.IOException;

import javax.swing.JPasswordField;

import ClientLogic.*;
import Messages.*;
import Messages.MessageLoginReply.ErrMsg;
import Messages.MessageLoginReply.LoginStatus;
 

/**
 * This is the Login Screen .

 *
 */
public class Login extends JPanel {

	
	private static final long serialVersionUID = 1L;
//	public JButton Close = null;
 
	public MainScreen ms=null;
	public MainMenu mm=null;
    private JButton Login = null;
	private JTextField Username = null;
	private JLabel User = null;
	private JLabel Pass = null;
	private JButton Exit = null;
	private JPasswordField jPasswordField = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	public JLabel imageLabel = null;
	
	
	/**
	 * This is the default constructor
	 * @param MS
	 */
	public Login(MainScreen MS) {
		super();
		this.ms = MS;
		initialize();
	}

	/**
	 * This method initializes the Login screen
	 */
	private void initialize() {

		this.setSize(900, 700);
						
		Pass = new JLabel();
		Pass.setSize(new Dimension(91, 17));
		Pass.setLocation(new Point(257, 262));
		Pass.setFont(new Font("Corbel", Font.BOLD | Font.ITALIC, 14));
		Pass.setText("Password:");
		User = new JLabel();
		User.setSize(new Dimension(84, 17));
		User.setLocation(new Point(257, 225));
		User.setFont(new Font("Corbel", Font.BOLD | Font.ITALIC, 14));
		User.setText("Username:");
		imageLabel = new JLabel(new ImageIcon("F:\\PROJECT\\ProjCode\\icon-roberg.png"));
	 	imageLabel.setLocation(603, 0);
		imageLabel.setSize(271,115);
		JLabel title = new JLabel("Roberg System");
		title.setFont(new Font("Serif", Font.BOLD, 48));
		title.setForeground(Color.BLACK);
		title.setBounds(581, 596, 278, 50);	  
		JLabel title1 = new JLabel(" Login");
		title1.setFont(new Font("Serif", Font.BOLD, 48));
		title1.setForeground(Color.BLACK);
		title1.setBounds(352, 107, 400, 62);
	
		this.setLayout(null);
		this.setBackground(Color.WHITE);
		this.add(getLogin(), null);
		this.add(getUsername(), null);
		this.add(User, null);
		this.add(Pass, null);
		this.add(getExit(), null);
		this.add(getJPasswordField(), null);		
	 	this.add(imageLabel); 	
	 	this.add(title);	 		 	
	 	this.add(title1);
	}
	
	
	
	
	/**
	 * This method initializes Username text field	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUsername() {
		if (Username == null) {
			Username = new JTextField("admin");
			Username.setPreferredSize(new Dimension(5, 25));
			Username.setSize(new Dimension(200, 21));
			Username.setFont(new Font("Dialog", Font.BOLD, 14));
			Username.setLocation(new Point(352, 225));
		}
		return Username;
	}
	
	
	
	
	/**
	 * This method initializes Password	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField("admin");    
			jPasswordField.setLocation(new Point(352, 260));
			jPasswordField.setFont(new Font("Dialog", Font.BOLD, 14));
			jPasswordField.setSize(new Dimension(200, 21));
		}
		return jPasswordField;
	}

	

	/**
	 * This method initializes Login	
	 * 	sends MessageLogin and receives MessageLoginReply messages .
	 * @return javax.swing.JButton	
	 */
	private JButton getLogin() {
		if (Login == null) {
		 	Login = new JButton();		 
		 	Login.setSize(new Dimension(95, 30));
		 	Login.setLocation(new Point(352, 295));
		 	Login.setText("Login");
			Login.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println(" Get login actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					MessageLogin msgLog=new MessageLogin(Username.getText(),jPasswordField.getText());
					ClientTaskAssignment cta=new ClientTaskAssignment();
					try 
					{						
						ms.client.sendToServer(msgLog);						
						cta.msg= (Message) ms.client.GetMessage();
					} catch (IOException e1) 
						{
							e1.printStackTrace();
						} 
				
					MessageLoginReply msglogrep=cta.getLoginReply();
                    System.out.println("Login"+msglogrep.getLoginStatus());
                  //  System.out.println("Permission"+msglogrep.getPermission());
                   // System.out.println("Error"+msglogrep.GetErrorMesage());
                    //int Success=0;
                    if(msglogrep.getLoginStatus()==LoginStatus.LOGGED_IN) 
                    {
                    	//Success=1;
                    	try {                   		
							mm= new MainMenu(ms,msglogrep.getUser());
							} catch (Exception e1) 
								{							
									e1.printStackTrace();
								}
                    	ms.dispose();
                    	//ms.setContentPane(mm);
                    }
                    if(msglogrep.getLoginStatus()==LoginStatus.LOGIN_FAIL)
                    {
                    	if(msglogrep.getError()==ErrMsg.ALREADY_LOGIN)
                    		JOptionPane.showMessageDialog(null,msglogrep.GetErrorMesage(2));
                    	else if(msglogrep.getError()==ErrMsg.INCORRECT)
                    		JOptionPane.showMessageDialog(null,msglogrep.GetErrorMesage(1));
                      	else
                    		JOptionPane.showMessageDialog(null,msglogrep.GetErrorMesage(3));
                    	
                    }
                /*    	JOptionPane.showMessageDialog(null,"Error, you are already logged in");
                    else
                    {
                    	if(Success==0)
                    		JOptionPane.showMessageDialog(null,"Error in username or password");
                    }*/
				}
			});
		}
		return Login;
	}

	/**
	 * This method initializes Exit	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getExit() {
		if (Exit == null) {
			Exit = new JButton();
			Exit.setText("Exit");
			Exit.setSize(new Dimension(95, 30));
			Exit.setActionCommand("Exit");
			Exit.setLocation(new Point(456, 295));
			Exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					JOptionPane.showMessageDialog(null,"Thanks For Using AES");
					System.exit(ABORT);
				}
			});
		}
		return Exit;
	}


}  
